﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


   public class CoNameHelper
    {
        #region 相关方法

        static string[] _firstName = new string[]{  
                    "白","毕","卞","蔡","曹","岑","常","车","陈","成" ,"程","池","邓","丁","范","方","樊","费","冯","符"
                   ,"傅","甘","高","葛","龚","古","关","郭","韩","何" ,"贺","洪","侯","胡","华","黄","霍","姬","简","江"
                   ,"姜","蒋","金","康","柯","孔","赖","郎","乐","雷" ,"黎","李","连","廉","梁","廖","林","凌","刘","柳"
                   ,"龙","卢","鲁","陆","路","吕","罗","骆","马","梅" ,"孟","莫","母","穆","倪","宁","欧","区","潘","彭"
                   ,"蒲","皮","齐","戚","钱","强","秦","丘","邱","饶" ,"任","沈","盛","施","石","时","史","司徒","苏","孙"
                   ,"谭","汤","唐","陶","田","童","涂","王","危","韦" ,"卫","魏","温","文","翁","巫","邬","吴","伍","武"
                   ,"席","夏","萧","谢","辛","邢","徐","许","薛","严" ,"颜","杨","叶","易","殷","尤","于","余","俞","虞"
                   ,"元","袁","岳","云","曾","詹","张","章","赵","郑" ,"钟","周","邹","朱","褚","庄","卓","东方","上官"
                   ,"令狐","申屠","欧阳" };

        static string _lastNameMale = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";

        static string _lastNameFeMa = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝鱼晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽墨";

        
       

        /// <summary>
        /// 返回指定范围内不重复的随机数组，如范围小于数组长度，则返回最多能生成的随机数组
        /// </summary>
        /// <param name="max"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        private static int[] GenerateNonRepeatArray(int max, int count)
        {
            if (max <= 0) { throw new Exception("范围不能等于或小于0"); }

            int size = count > max ? count - max : 0;

            List<int> temp = new List<int>();
            Random ran = new Random();

            while (temp.Count < (count - size))
            {
                int item = ran.Next(max);
                if (!temp.Contains(item))
                {
                    temp.Add(item);
                }
            }

            return temp.ToArray();
        }

        /// <summary>
        /// 取单个字符的拼音声母
        /// </summary>
        /// <param name="c">要转换的单个汉字</param>
        /// <returns>拼音声母</returns>
        private static string GetPYChar(string c)
        {

            byte[] array = new byte[2];

            array = System.Text.Encoding.Default.GetBytes(c);

            int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));

            if (i < 0xB0A1) return "*";

            if (i < 0xB0C5) return "a";

            if (i < 0xB2C1) return "b";

            if (i < 0xB4EE) return "c";

            if (i < 0xB6EA) return "d";

            if (i < 0xB7A2) return "e";

            if (i < 0xB8C1) return "f";

            if (i < 0xB9FE) return "g";

            if (i < 0xBBF7) return "h";

            if (i < 0xBFA6) return "g";

            if (i < 0xC0AC) return "k";

            if (i < 0xC2E8) return "l";

            if (i < 0xC4C3) return "m";

            if (i < 0xC5B6) return "n";

            if (i < 0xC5BE) return "o";

            if (i < 0xC6DA) return "p";

            if (i < 0xC8BB) return "q";

            if (i < 0xC8F6) return "r";

            if (i < 0xCBFA) return "s";

            if (i < 0xCDDA) return "t";

            if (i < 0xCEF4) return "w";

            if (i < 0xD1B9) return "x";

            if (i < 0xD4D1) return "y";

            if (i < 0xD7FA) return "z";

            return "*";

        }
        #endregion

        /// <summary>
        /// 身份证
        /// </summary>
        public static string GetAIDCardNumber()
        {

            System.Random rnd;
            string[] _crabodistrict = new string[] { "350201", "350202", "350203", "350204", "350205", "350206", "350211", "350205", "350213" };


            rnd = new Random(System.DateTime.Now.Millisecond);

            //PIN = District + Year(50-92) + Month(01-12) + Date(01-30) + Seq(001-600)
            string _pinCode = string.Format("{0}19{1}{2:00}{3:00}{4:000}", _crabodistrict[rnd.Next(0, 8)], rnd.Next(50, 92), rnd.Next(1, 12), rnd.Next(1, 30), rnd.Next(1, 600));
            #region Verify
            char[] _chrPinCode = _pinCode.ToCharArray();
            //校验码字符值
            char[] _chrVerify = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
            //i----表示号码字符从由至左包括校验码在内的位置序号；
            //ai----表示第i位置上的号码字符值；
            //Wi----示第i位置上的加权因子，其数值依据公式intWeight=2（n-1）(mod 11)计算得出。
            int[] _intWeight = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
            int _craboWeight = 0;
            for (int i = 0; i < 17; i++)//从1 到 17 位,18为要生成的验证码
            {
                _craboWeight = _craboWeight + Convert.ToUInt16(_chrPinCode[i].ToString()) * _intWeight[i];
            }
            _craboWeight = _craboWeight % 11;
            _pinCode += _chrVerify[_craboWeight];
            #endregion
            string idcard = _pinCode;
            return idcard;
        }
        /// <summary>
        /// 生成拼音
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetPYString(string str)
        {

            string tempStr = "";

            foreach (char c in str)
            {

                if ((int)c >= 33 && (int)c <= 126)
                {//字母和符号原样保留

                    tempStr += c.ToString();

                }

                else
                {//累加拼音声母

                    tempStr += GetPYChar(c.ToString());

                }

            }

            return tempStr;

        }

        /// <summary>
        /// 生成拼音,但是不保留拼音和字母
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetPYStringNoZM(string str)
        {

            string tempStr = "";

            foreach (char c in str)
            {

                if ((int)c >= 33 && (int)c <= 126)
                {//字母和符号不保留

                   // tempStr += c.ToString();

                }

                else
                {//累加拼音声母

                    tempStr += GetPYChar(c.ToString());

                }

            }

            return tempStr;

        }

        /// <summary>
        /// 获取多个名字
        /// </summary>
        /// <param name="sex"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<string> GetNames(bool? sex, int count)
        {
            int fLength = _firstName.Length;
            int lLength = 0;

            string nameSource = string.Empty;
            if (sex.HasValue)
            {
                lLength = sex.Value ? _lastNameMale.Length : _lastNameFeMa.Length;
                nameSource = sex.Value ? _lastNameMale : _lastNameFeMa;
            }
            else
            {
                lLength = _lastNameMale.Length + _lastNameFeMa.Length;
                nameSource = string.Concat(_lastNameMale, _lastNameFeMa);
            }

            List<string> names = new List<string>();

            System.Threading.Thread.Sleep(20);
            int[] Indexfn = GenerateNonRepeatArray(count, count);
            System.Threading.Thread.Sleep(20);
            int[] Indexln1 = GenerateNonRepeatArray(count, count);
            System.Threading.Thread.Sleep(20);
            int[] Indexln2 = GenerateNonRepeatArray(count, count);

            int nameLength = nameSource.Length;

            for (int i = 0; i < count; i++)
            {
                int v1 = Indexfn[i];
                int v2 = Indexln1[i];
                int v3 = Indexln2[i];

                if (v1 >= fLength)
                {
                    int j = Indexfn[i] / fLength;
                    v1 = Indexfn[i] - fLength * j;
                }
                if (v2 >= nameLength)
                {
                    int j = Indexln1[i] / nameLength;
                    v2 = Indexln1[i] - nameLength * j;
                }
                if (v3 >= nameLength)
                {
                    int j = Indexln2[i] / nameLength;
                    v3 = Indexln2[i] - nameLength * j;
                }

                if (v1 < 0 || v2 < 0 || v3 < 0)
                {
                    Console.WriteLine();
                }

                if (v1 >= _firstName.Length || v2 >= nameSource.Length || v3 >= nameSource.Length)
                {
                    Console.WriteLine();
                }

                string n1 = string.Empty;
                string n2 = string.Empty;
                string n3 = string.Empty;

                names.Add(string.Format("{0}{1}{2}", _firstName[v1], nameSource[v2], nameSource[v3]));

            }

            return names;
        }
        /// <summary>
        /// 获取一个名字
        /// </summary>
        /// <param name="sex"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string GetANames(bool? sex, int count)
        {

            int fLength = _firstName.Length;
            int lLength = 0;

            string nameSource = string.Empty;
            if (sex.HasValue)
            {
                lLength = sex.Value ? _lastNameMale.Length : _lastNameFeMa.Length;
                nameSource = sex.Value ? _lastNameMale : _lastNameFeMa;
            }
            else
            {
                lLength = _lastNameMale.Length + _lastNameFeMa.Length;
                nameSource = string.Concat(_lastNameMale, _lastNameFeMa);
            }

            List<string> names = new List<string>();

            System.Threading.Thread.Sleep(20);
            int[] Indexfn = GenerateNonRepeatArray(count, count);
            System.Threading.Thread.Sleep(20);
            int[] Indexln1 = GenerateNonRepeatArray(count, count);
            System.Threading.Thread.Sleep(20);
            int[] Indexln2 = GenerateNonRepeatArray(count, count);

            int nameLength = nameSource.Length;

            for (int i = 0; i < 1; i++)
            {
                int v1 = Indexfn[i];
                int v2 = Indexln1[i];
                int v3 = Indexln2[i];

                if (v1 >= fLength)
                {
                    int j = Indexfn[i] / fLength;
                    v1 = Indexfn[i] - fLength * j;
                }
                if (v2 >= nameLength)
                {
                    int j = Indexln1[i] / nameLength;
                    v2 = Indexln1[i] - nameLength * j;
                }
                if (v3 >= nameLength)
                {
                    int j = Indexln2[i] / nameLength;
                    v3 = Indexln2[i] - nameLength * j;
                }

                if (v1 < 0 || v2 < 0 || v3 < 0)
                {
                    Console.WriteLine();
                }

                if (v1 >= _firstName.Length || v2 >= nameSource.Length || v3 >= nameSource.Length)
                {
                    Console.WriteLine();
                }

                string n1 = string.Empty;
                string n2 = string.Empty;
                string n3 = string.Empty;

                names.Add(string.Format("{0}{1}{2}", _firstName[v1], nameSource[v2], nameSource[v3]));

            }

            return names.OrderBy(p => Guid.NewGuid()).Take(1).First();
        }

        /// <summary>
        /// 从字符串里随机得到，规定个数的字母.
        /// </summary>
        /// <param name="allChar"></param>
        /// <param name="CodeCount"></param>
        /// <returns></returns>
        public static string GetRandomZM(int CodeCount)
        {
            string allChar = "A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; 
            string[] allCharArray = allChar.Split(',');
            string RandomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < CodeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
                }

                int t = rand.Next(allCharArray.Length - 1);

                while (temp == t)
                {
                    t = rand.Next(allCharArray.Length - 1);
                }

                temp = t;
                RandomCode += allCharArray[t];
            }
            return RandomCode;
        }




    }

